package kamkor.webtools.url

import scala.util.matching.Regex
import java.util.regex.Matcher

/**
 * Swaps/replaces urls. For example if text has images on server x, as:
 * 
 * http://www.x.com/1.jpg etc., then this class gives tools for swapping
 * urls to for example http://www.y.com/1.jpg  
 * 
 * 
 * @author kamkor
 *
 */
object UrlSwapper {	
	/**
	 * Replaces all urls in text with newUrl. Only host part of url is replaced. Path stays the same	 
	 * 
	 * Urls must start with http:// or https://	  
	 * Urls must end properly:
	 *   http://www.website.comhttp://www.otherwebsite.pl is wrong. 
	 *   http://www.website.com http://www.otherwebsite.pl is correct	 
	 *   
	 * @param text text with urls
	 * @param newUrl for example http://www.drift.pl
	 * @return text with replaced urls
	 */
	def replaceAllUrlHostOnly(text: String, newUrl: String): String = { 
		UrlPatterns.hostPattern.r.replaceAllIn(text, fixUrlForHostSwap(newUrl))
	}
	
	/**
	 * Replaces all urls in text with newUrl. Only host part of url is replaced, path stays the same.
	 * 
	 * Url is replaced only if it ends with one of the required extensions
	 * 
	 * @param text with urls
	 * @param newUrl ie. http://www.drift.pl
	 * @param extensions required extensions, ie: jpg png gif
	 * @return text with replaced urls
	 */
	def replaceAllUrlHostOnly(text: String, newUrl: String, extensions: Array[String]): String = {	
		val fixedNewUrl = fixUrlForHostSwap(newUrl)
		// replaceAll urls in text
		UrlPatterns.hostAndPathAndFilePattern.r.replaceAllIn(text, m => {
			// get url to be replaced (if it contains specified extension)
			var url = m.toString()			
			for (ext <- extensions
				 if url.matches("""(?i).*"""+ ext +"""$""")) { // if url ends with specified ext then replace its host				
				url = UrlPatterns.hostPattern.r.replaceFirstIn(url, fixedNewUrl)				
			}	
			Matcher.quoteReplacement(url)
		});		
	}
	
	/**
	 * Replaces all urls in text with newUrl. Host and its path are replaced. 
	 * 
	 * Urls in text must start with http:// or https://	  
	 * Urls must end properly:
	 *   http://www.website.comhttp://www.otherwebsite.pl is wrong. 
	 *   http://www.website.com http://www.otherwebsite.pl is correct
	 * 
	 * @param text with urls
	 * @param newUrl for example http://www.drift.pl
	 * @return text with replaced urls
	 */
	def replaceAllUrlPathIncluded(text: String, newUrl: String): String = {
		UrlPatterns.hostAndPathPattern.r.replaceAllIn(text, fixUrlForHostAndPathSwap(newUrl))
	}
	
	/**
	 * Replaces all urls in text with newUrl. Host and its path are replaced. 
	 * 
	 * Url is replaced only if it ends with one of the required extensions
	 * 
	 * @param text with urls
	 * @param newUrl ie. http://www.drift.pl
	 * @param extensions required extensions, ie: jpg png gif
	 * @return text with replaced urls
	 */
	def replaceAllUrlPathIncluded(text: String, newUrl: String, extensions: Array[String]): String = {		
		val fixedNewUrl = fixUrlForHostAndPathSwap(newUrl)		
		// replaceAll urls in text
		UrlPatterns.hostAndPathAndFilePattern.r.replaceAllIn(text, m => {
			// get url to be replaced (if it contains specified extension)
			var url = m.toString()			
			for (ext <- extensions
				 if url.matches("""(?i).*"""+ ext +"""$""")) { // if url ends with specified ext then replace its host and path				
				url = UrlPatterns.hostAndPathPattern.r.replaceFirstIn(url, fixedNewUrl)				
			}	
			Matcher.quoteReplacement(url)
		});		
	}
	
	
	/**
	 * Url for host swap must not end with slash "/"
	 * 
	 * @return url without slash "/" at the end
	 */
	private def fixUrlForHostSwap(url: String): String = { 
		if (!url.endsWith("/")) url else fixUrlForHostSwap(url.substring(0, url.length() - 1))
	}

	/**
	 * Url for host and path swap must end with slash "/". It also can't end with more than one slash, ie. "/////"
	 * 
	 * @return url with slash "/" at the end
	 */
	private def fixUrlForHostAndPathSwap(url: String): String = {
		def cutExtraSlashes(u: String): String = if (u.endsWith("//")) cutExtraSlashes(u.substring(0, u.length() - 1)) else u
		
		if (!url.endsWith("/"))  
			url + "/"		
		else if (url.endsWith("//")) 
			cutExtraSlashes(url)
		else
			url			
	}
}
